package com.kunya.product.service.impl.product;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.product.ProductCollectionMapper;
import com.kunya.product.po.Product;
import com.kunya.product.po.ProductCollection;
import com.kunya.product.service.category.IProductCategoryService;
import com.kunya.product.service.product.IProductCollectionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fzl
 * @Description: 商品收藏 接口层
 * @date 2018/7/5 11:33
 */
@Service("iProductCollectionService")
public class ProductCollectionServiceImpl implements IProductCollectionService {
    @Autowired
    private IProductCategoryService ProductService;
    @Autowired
    private ProductCollectionMapper productCollectionMapper;

    public ServerResponse collectionProduct(Long productSpecNumber, Long userId, String tablePrefix){
        if (null==productSpecNumber||null==userId|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }

        int reusltCount=productCollectionMapper.collectionProduct(productSpecNumber, userId, tablePrefix);
        if(reusltCount<=0){
            return ServerResponse.createByErrorMessage("添加收藏失败");
        }
        return  ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse getCollection(Long productSpecNumber,Long userId,String tablePrefix) {
        if (null==productSpecNumber||null==userId|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        ProductCollection productCollection=productCollectionMapper.getCollection(productSpecNumber,userId,tablePrefix);
        return ServerResponse.createBySuccess(productCollection);
    }

    @Override
    public  ServerResponse<List<Product>> getCollerctionProduct(Long userId, String tablePrefix) {
        if (null==userId|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        List<Product> list=productCollectionMapper.getCollerctionProduct(userId,tablePrefix);
        return  ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse deleteCollection(Long userId, Long productSpecNumber, String tablePrefix) {
        if (null==productSpecNumber||null==userId|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        int reusltCount= productCollectionMapper.deleteCollection(userId,productSpecNumber,tablePrefix);
        if(reusltCount<=0){
            return ServerResponse.createByErrorMessage("删除收藏失败");
        }
        return  ServerResponse.createBySuccess();
    }

}

